package it.marcomondini.proximitytools.receivers;

import it.marcomondini.proximitytools.AccessTableAdapter;
import it.marcomondini.proximitytools.ValidateActivity;
import it.marcomondini.proximitytools.facades.NotificationFacade;
import it.marcomondini.proximitytools.powermanagement.WakeFragment;
import it.marcomondini.proximitytools.powermanagement.WakeRunner;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.os.PowerManager;

public class ProximityReceiver extends BroadcastReceiver {
	
	private static final int NOTIFICATION_ID = 1000;

	@Override
	public void onReceive(final Context ctx, Intent intent) {
		
		final boolean entering = intent.getBooleanExtra(LocationManager.KEY_PROXIMITY_ENTERING, false);
		AccessTableAdapter ata  = new AccessTableAdapter(ctx);
		ata.open();
		ata.createNote(entering, System.currentTimeMillis());
		ata.close();
		WakeRunner.acquireCpuWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK| PowerManager.ACQUIRE_CAUSES_WAKEUP, new WakeFragment() {
			
			@Override
			public void wakeRun() {
				PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, new Intent(ctx, ValidateActivity.class), PendingIntent.FLAG_ONE_SHOT);		
				NotificationFacade.instance().forward(ctx, NOTIFICATION_ID, pendingIntent, "Notified!");
			}
		});
		

	}
	
	
}
